package se.webbzon.boltzmann.effect;

import java.awt.Color;
import java.util.ArrayList;

import javax.media.j3d.Transform3D;
import javax.vecmath.AxisAngle4d;

import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;
import se.webbzon.oschi01.worldobject.appearance.ConeAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.GroupAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;

public class MarkerEffect extends WorldStaticObject implements Effect {
	
	// The parent of this marker effect
	private final WorldStaticObject parent;
	
	// The phase of the marker oscillation
	private double t;
	
	/*============================================================
	Constructors
	============================================================*/

	public MarkerEffect(WorldStaticObject parent, Color color) {
		super(WorldObject.onStep, createAppearance(color));
		this.parent = parent;
		t = 0;
		
		setPosition(parent.getX(),
					parent.getY() + parent.getHeight()/2 + 16,
					parent.getZ());
		setSize(8,8,8);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void launch(WorldArea world) {
		world.createInstance(this);
	}

	@Override public void terminate(WorldArea world) {
		world.destroyInstance(this);
	}
	
	@Override public void onStep(WorldArea world) {
		t += Math.PI/45;
		setPosition(parent.getX(),
					parent.getY() + parent.getHeight()/2 + 16 + 2*Math.sin(t),
					parent.getZ());
		setRotation(0,1,0,t);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	private static final Appearance3D createAppearance(Color color) {
		ArrayList<Appearance3D> appearances = new ArrayList<Appearance3D>();
		
		// Create marker appearance
		SimpleAppearance3D marker = new ConeAppearance3D(false,4);
		marker.setColor(color);
		Transform3D transform = new Transform3D();
		transform.setRotation(new AxisAngle4d(0,0,1,Math.PI));
		marker.setTransform(transform);
		appearances.add(marker);
		
		return new GroupAppearance3D(appearances);
	}

}
